package main;


import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;

import com.jfinal.plugin.activerecord.Record;

import wordUtil.docExport.WordExportUtil;

/**
 * 导出docx文件
 */
public class MainExportDocx {

	public static Logger logger = Logger.getLogger(MainExportDocx.class);

	public static void main(String[] args) throws DocumentException, IOException {
		exportDocx(); // docx导出
	}

	private static void exportDocx() throws IOException {

		Class<MainExportDocx> clazz = MainExportDocx.class;
		URL resource = clazz.getResource("/docx_template.docx");

		String path = URLDecoder.decode(resource.getPath(), "UTF-8");

		List<Record> param = getExportData(); // 获取导出数据

		String outFilePath = "D://gen2_result.docx";

		WordExportUtil.generateWord(param, path, outFilePath);

		System.out.println("ok!");
	}

	private static List<Record> getExportData() {
		// 数据
		List<Record> param = new ArrayList<Record>();
		{
			// 单个数据
			Record record = new Record();
			record.set("key_", "biandianzhan");
			record.set("value", "220kV佛光变电站");
			param.add(record);

			// 重复数据
			Record group = new Record();
			group.set("isgroup", 1);
			List<List<Record>> groupList = new ArrayList<>();

			// 第一组数据
			{
				List<Record> childs = new ArrayList<>();
				Record child1 = new Record();
				child1.set("key_", "key_jiangemingcheng");
				child1.set("value", "1#主变间隔");
				childs.add(child1);

				Record child2 = new Record();
				child2.set("key_", "key_shebeimingcheng");
				child2.set("value", "1#主变201断路器");
				childs.add(child2);

				Record child3 = new Record();
				child3.set("key_", "key_huanjingwendu");
				child3.set("value", "25");
				childs.add(child3);

				groupList.add(childs);
			}

			// 第二组数据
			{
				List<Record> childs = new ArrayList<>();
				Record child1 = new Record();
				child1.set("key_", "key_jiangemingcheng");
				child1.set("value", "1#主变间隔2");
				childs.add(child1);

				Record child2 = new Record();
				child2.set("key_", "key_shebeimingcheng");
				child2.set("value", "1#主变201断路器2");
				childs.add(child2);

				groupList.add(childs);
			}
			// 第三组数据
			{
				List<Record> childs = new ArrayList<>();
				Record child1 = new Record();
				child1.set("key_", "key_jiangemingcheng");
				child1.set("value", "1#主变间隔3");
				childs.add(child1);

				Record child2 = new Record();
				child2.set("key_", "key_shebeimingcheng");
				child2.set("value", "1#主变201断路器3");
				childs.add(child2);

				groupList.add(childs);
			}

			group.set("data", groupList);
			param.add(group);
		}
		return param;
	}

}
